package com.example.takephotovideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.takephotovideo.R;
import com.example.takephotovideo.customview.TakePhotoView;
import com.example.takephotovideo.takephotovideo.PhotoVideoListBean;
import com.example.takephotovideo.takephotovideo.RvListBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * @功能:
 * @Creat 2020/3/18 11:42
 * @User Lmy
 * @Compony JinAnChang
 */
public class RecyclerviewTakePhotoAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<RvListBean.ResultDataBean> fileListBeans;

    public RecyclerviewTakePhotoAdapter(Context context, List<RvListBean.ResultDataBean> fileListBeans) {
        this.context = context;
        this.fileListBeans = fileListBeans;
    }

    @Override
    public int getItemCount() {
        return fileListBeans.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.revyvlerview_takephoto_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, List payload) {
        if (payload.isEmpty()) {
            ((ViewHolder) holder).title.setText(fileListBeans.get(position).getName());
            List<PhotoVideoListBean> photoVideoListBeanList = new ArrayList<>();
            for (int i = 0; i < fileListBeans.get(position).getPhotoVideoListBeanList().size(); i++) {
                photoVideoListBeanList.add(new Gson().fromJson(new Gson().toJson(fileListBeans.get(position).getPhotoVideoListBeanList().get(i)), PhotoVideoListBean.class));
            }
            ((ViewHolder) holder).takephotoview.setDataList(photoVideoListBeanList);
            ((ViewHolder) holder).takephotoview.setOnItemClickListener(new TakePhotoView.OnItemClickListener() {
                @Override
                public void addPhotoVideo(int surplusNumber) {
                    onItemClickListener.addPhotoVideo(surplusNumber, position);
                }
            });
        } else {
            int type = (int) payload.get(0);// 刷新哪个部分 标志位
            switch (type) {
                case 3:
                    List<PhotoVideoListBean> photoVideoListBeanList = new ArrayList<>();
                    for (int i = 0; i < fileListBeans.get(position).getPhotoVideoListBeanList().size(); i++) {
                        photoVideoListBeanList.add(new Gson().fromJson(new Gson().toJson(fileListBeans.get(position).getPhotoVideoListBeanList().get(i)), PhotoVideoListBean.class));
                    }
                    ((ViewHolder) holder).takephotoview.setDataList(photoVideoListBeanList);
                    break;
            }
        }
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void addPhotoVideo(int surplusNumber, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TakePhotoView takephotoview;
        private TextView title;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            takephotoview = itemView.findViewById(R.id.takephotoview);
        }
    }
}
